#include "IOSetWin.h"

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;

/**数据的输入框**/
static lv_obj_t * spinbox_1;
static lv_obj_t * spinbox_2;
static lv_obj_t * spinbox_3;
static lv_obj_t * spinbox_4;


/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}

/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
    motor_set_io_speed1(lv_spinbox_get_value(spinbox_1));
    motor_set_io_speed2(lv_spinbox_get_value(spinbox_2));
    motor_set_io_speed3(lv_spinbox_get_value(spinbox_3));
    motor_set_io_speed4(lv_spinbox_get_value(spinbox_4));
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(4);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(4);
}

/**切换数字输入框的编辑状态 回调函数**/
static void spinbox_cb(lv_event_t * event)
{
    /**判断是否是编辑状态**/
	if(lv_group_get_editing(keyGroup))
	{
		//printf("LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, false);
	}
	else
	{
		//printf("!LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, true);
		//printf("set LV_STATE_FOCUSED\r\n");
	}
}

void open_IOsetWin()
{
    lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

    lv_obj_set_scrollbar_mode(scr,LV_SCROLLBAR_MODE_OFF);


    lv_obj_t* label = lv_label_create(lv_scr_act());
    lv_label_set_text(label,"速1");
    lv_obj_set_style_text_font(label,&yahei_16,LV_PART_MAIN);
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,20-15);

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label,"速2");
    lv_obj_set_style_text_font(label,&yahei_16,LV_PART_MAIN);
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,40-12);

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label,"速3");
    lv_obj_set_style_text_font(label,&yahei_16,LV_PART_MAIN);
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,60-10);

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label,"速4");
    lv_obj_set_style_text_font(label,&yahei_16,LV_PART_MAIN);
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,80,20-15);

    spinbox_1 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_1,LV_ALIGN_TOP_LEFT,27,23-15);
    lv_obj_set_width(spinbox_1,45);
    lv_obj_set_height(spinbox_1,16);
    lv_obj_set_style_pad_bottom(spinbox_1,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_1,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_1, -65535, 65535);
    lv_spinbox_set_digit_format(spinbox_1, 5, 0);
	lv_spinbox_set_step(spinbox_1,1);
    lv_spinbox_set_value(spinbox_1,motor_get_io_speed1());
    lv_obj_add_event_cb(spinbox_1,spinbox_cb,LV_EVENT_PRESSED,NULL);

    spinbox_2 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_2,LV_ALIGN_TOP_LEFT,27,43-12);
    lv_obj_set_width(spinbox_2,45);
    lv_obj_set_height(spinbox_2,16);
    lv_obj_set_style_pad_bottom(spinbox_2,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_2,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_2, -65535, 65535);
    lv_spinbox_set_digit_format(spinbox_2, 5, 0);
	lv_spinbox_set_step(spinbox_2,1);
    lv_spinbox_set_value(spinbox_2,motor_get_io_speed2());
    lv_obj_add_event_cb(spinbox_2,spinbox_cb,LV_EVENT_PRESSED,NULL);

    spinbox_3 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_3,LV_ALIGN_TOP_LEFT,27,63-10);
    lv_obj_set_width(spinbox_3,45);
    lv_obj_set_height(spinbox_3,16);
    lv_obj_set_style_pad_bottom(spinbox_3,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_3,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_3, -65535, 65535);
    lv_spinbox_set_digit_format(spinbox_3, 5, 0);
	lv_spinbox_set_step(spinbox_3,1);
    lv_spinbox_set_value(spinbox_3,motor_get_io_speed3());
    lv_obj_add_event_cb(spinbox_3,spinbox_cb,LV_EVENT_PRESSED,NULL);

    spinbox_4 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_4,LV_ALIGN_TOP_LEFT,110,23-15);
    lv_obj_set_width(spinbox_4,45);
    lv_obj_set_height(spinbox_4,16);
    lv_obj_set_style_pad_bottom(spinbox_4,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_4,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_4, -65535, 65535);
    lv_spinbox_set_digit_format(spinbox_4, 5, 0);
	lv_spinbox_set_step(spinbox_4,1); 
    lv_spinbox_set_value(spinbox_4,motor_get_io_speed4()); 
    lv_obj_add_event_cb(spinbox_4,spinbox_cb,LV_EVENT_PRESSED,NULL);

	lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn1);
	lv_label_set_text(label, "Enter");
	lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, 65, 16);
	lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 80, 42-12);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Cancel");
	lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, 65, 16);
	lv_obj_align(btn2, LV_ALIGN_TOP_LEFT, 80, 62-10);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL);    


    keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

    lv_group_add_obj(keyGroup,spinbox_1);
    lv_group_add_obj(keyGroup,spinbox_2);
    lv_group_add_obj(keyGroup,spinbox_3);
    lv_group_add_obj(keyGroup,spinbox_4);
    lv_group_add_obj(keyGroup,btn1);
    lv_group_add_obj(keyGroup,btn2);
}